package com.bizdk.generator.core.query;

import cn.hutool.core.util.StrUtil;
import com.bizdk.generator.core.DbType;

/**
 * MySQL查询
 */
public class MySqlQuery implements DialectQuery {
    
    @Override
    public DbType dbType() {
        return DbType.MySQL;
    }
    
    @Override
    public String tableSql(String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("select ") //
                .append("table_name, ") //
                .append("table_comment ") //
                .append("from information_schema.tables ") //
                .append("where table_schema = (select database()) ");
        // 表名查询
        if (StrUtil.isNotBlank(tableName)) {
            sql.append("and table_name = '").append(tableName).append("' ");
        }
        sql.append("order by table_name asc");
        
        return sql.toString();
    }
    
    @Override
    public String tableName() {
        return "table_name";
    }
    
    @Override
    public String tableComment() {
        return "table_comment";
    }
    
    @Override
    public String columnSql() {
        StringBuilder sql = new StringBuilder();
        sql.append("select ") //
                .append("column_name, ") //
                .append("data_type, ") //
                .append("column_comment, ") //
                .append("column_key ") //
                .append("from information_schema.columns ") //
                .append("where table_name = '%s' and table_schema = (select database()) order by ordinal_position");
        return sql.toString();
    }
    
    @Override
    public String columnName() {
        return "column_name";
    }
    
    @Override
    public String columnType() {
        return "data_type";
    }
    
    @Override
    public String columnComment() {
        return "column_comment";
    }
    
    @Override
    public String columnKey() {
        return "column_key";
    }
}
